{block head}
<script type="text/javascript" n:syntax="double">
    var index = 0;
    var estates = [{{foreach $estates as $estateId}}{{$estateId}}{{if !$iterator->last}},{{/if}}{{/foreach}}];    
    var numOfSteps = 0;
    
    jQuery.extend({
	reality: {
            updateSnippet: function (id, html) {
                var currHtml = $("#" + id).html();
                $("#" + id).html(currHtml + html);
            },                                
            
            error: function () {
                hideBar();                                        
                $("#import-status").html($("#import-status").html() 
                        + " chyba");
            },                                
            
            success: function (payload) {
                hideBar();                                        
                
                index++;
                if (index < numOfSteps) {
                    $("#import-status").html($("#import-status").html() + "<br /><span>" 
                        + estates[index] + " [" + (index + 1) + "/" + numOfSteps + "]</span>");
                    
                    showBar();
                    $.get("/nemovitosti/import/" + estates[index] + "?do=importOne", jQuery.reality.success)
                        .error(jQuery.reality.error);
                } else {
                    $("#import-status").html($("#import-status").html() 
                        + "<br /><br /><span>všechny nemovitosti naimportovány</span>");
                }
            }
	}
    });
    
    $("#import-button").live("click", function (event) {
        event.preventDefault();         
        numOfSteps = estates.length;
        index = 0;        
        $("#import-status").html("<span>Stav importu</span><br />");
        $("#import-status").html($("#import-status").html() + "<br /><span>" 
            + estates[0] + " [" + (index + 1) + "/" + numOfSteps + "]</span>");
        showBar();
        $.get("/nemovitosti/import/" + estates[0] + "?do=importOne", jQuery.reality.success)
            .error(jQuery.reality.error);
    });
    
    $(function() {
        $('<div id="ajax-bar"></div>').appendTo("body").hide();
    });
    
    function showBar () {
        var el = $("#import-status span").last();
        var offset = el.offset();
        var width = el.width();
        
        $("#ajax-bar").show().css({
            position: "absolute",
            left: offset.left + width + 10,
            top: offset.top
        });
    }
    
    function hideBar () {
        $("#ajax-bar").hide();
    }
</script>
{/block}

{block content}
<h1>Import</h1>
<a href="#" id="import-button">importovat</a>
<p id="import-status">
</p>
{/block}
